/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import com.mysql.jdbc.CallableStatement;
import dbhelper.dbconnect;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import POJO.traHangPOJO;
import java.util.Date;
/**
 *
 * @author SoftVnn Member
 */
public class traHangDAO {
    public ArrayList<traHangPOJO> getAll()
    {
        ArrayList<traHangPOJO> arr = new ArrayList<traHangPOJO>();
        try
        {
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spTraHang_getAll ();");
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                traHangPOJO obj = new traHangPOJO();
                obj.setMaTraHang(rs.getString("MaTraHang"));
                obj.setMaOrder(rs.getString("MaOrder"));
                obj.setMaSanPham(rs.getString("MaSanPham"));                
                obj.setThoiGianTra(rs.getDate("ThoiGianTra"));
                obj.setSoLuongTra(rs.getInt("SoLuongTra"));
                obj.setLyDo(rs.getString("Lydo"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error;
        }
        return arr;
    }
    public ArrayList<traHangPOJO> get(String MaTraHang)
    {
        ArrayList<traHangPOJO> arr = new ArrayList<traHangPOJO>();
        try
        {           
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spTraHang_get('%s');", MaTraHang);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                traHangPOJO obj = new traHangPOJO();
                obj.setMaTraHang(rs.getString("MaTraHang"));
                obj.setMaOrder(rs.getString("MaOrder"));
                obj.setMaSanPham(rs.getString("MaSanPham"));                
                obj.setThoiGianTra(rs.getDate("ThoiGianTra"));
                obj.setSoLuongTra(rs.getInt("SoLuongTra"));
                obj.setLyDo(rs.getString("Lydo"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return arr;
    }
//    public int insert(String MaTraHang, String MaOrder, String MaSanPham,Date ThoiGianTra, int SoLuongTra, String Lydo)
//    {
//        int result = 0;
//        try{
//            dbconnect con = new dbconnect();
//            con.open();
//            String sql = String.format("call spTraHang_insert('%s','%s','%s');", MaTraHang,MaOrder, MaSanPham);
//            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
//            if(st.execute()== false)
//            {
//                result = 1;
//            }
//            con.close();
//        }
//        catch(SQLException ex)
//        {
//            //error
//        }
//        return result;
//    }
//    public int update(String MaLoaiSanPham, String TenLoaiSanPham, String MaNhom)
//    {
//        int result = 0;
//        try{
//            dbconnect con = new dbconnect();
//            con.open();
//            String sql = String.format("call spLoaiSanPham_update('%s','%s','%s');", MaLoaiSanPham,TenLoaiSanPham, MaNhom);
//            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
//            if(st.execute()== false)
//            {
//                result = 1;
//            }
//            con.close();
//        }
//        catch(SQLException ex)
//        {
//            //error
//        }
//        return result;
//    }
    public int delete(String MaTraHang)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spTraHang_delete('%s');", MaTraHang);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public String  issue()
    {
        String MaTraHangMoi = "null";
        try
        {
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spTraHang_issue();");
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {                
                MaTraHangMoi = rs.getString("MaLoaiSanPhamMoi");
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error;
        }
        return MaTraHangMoi;
    }
}
